use constant {
	RDF => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
	RDFS => "http://www.w3.org/2000/01/rdf-schema#"
	  # other namespaces might go here
};

use Config::YAML;
my $cfg_file = shift @ARGV or die "config file expected as sole parameter";
my $config = Config::YAML->new( config => $cfg_file );
my $datfile_dir = "./data/dat/";
# Dumper is cool for debugging
#use Data::Dumper;
#warn Dumper $config;   # what did we actually get?

use XML::Writer;    # by default, XML::Writer objects output to STDOUT
#use IO::File;		# only for testing purposes...

#my $output = new IO::File(">temporaryRDFOutput.xml"); 		# uncomment if you want to write to a file
my $lvs = "http://tuwien.ac.at/lvs/";
my $dc = "http://purl.org/dc/elements/1.1/";
my $xml = "http://www.w3.org/XML/1998/namespace";
my $writer = new XML::Writer(
#	OUTPUT => $output,			   		# uncomment if you want to write to a file
	OUTPUT => $stdout,
	NAMESPACES => 1,                       # yes, we are namespace aware
	NEWLINE => 1,
	PREFIX_MAP => { main::RDF => 'rdf', $lvs => 'lvs', $dc => 'dc'},    # and this is the map to use to generate prefixes
	FORCED_NS_DECLS => [RDF, $dc, $lvs]
);       # and this is the list of namespace declarations we want at the top
$writer->xmlDecl("ISO-8859-1");
$writer->startTag( [ RDF, 'RDF' ] );

### STUDIUM.DAT ###

open FILE, $datfile_dir . $config->get_TitelderStudien or die $!;

while ( my $line = <FILE> ) {
	@linecontents = split( /\t/, $line );
	### GO ON HERE
	$writer->startTag( [ $lvs, 'Study' ], [ RDF, 'about'] => $lvs . 'study#' . @linecontents[1] );
		$writer->startTag( [ $dc, 'title' ] );
			$writer->characters( @linecontents[2] );
		$writer->endTag( [ $dc, 'title' ] );
		$writer->startTag( [ $lvs, 'StudyInternalID' ]);
			$writer->characters( @linecontents[0] );
		$writer->endTag( [ $lvs, 'StudyInternalID' ] );
	$writer->endTag( [ $lvs, 'Study' ] );
}

close FILE;

### PRÜFUNGSFACH.DAT ###

open FILE, $datfile_dir . $config->get_Pruefungsfachtitel or die $!;

while ( my $line = <FILE> ) {
	@linecontents = split( /\t/, $line );
	$writer->startTag( [ $lvs, 'ExamCourse' ], [ RDF, 'about'] => $lvs . 'examcourse#' . @linecontents[0] );
		$writer->startTag( [ $dc, 'title' ] );
			$writer->characters( @linecontents[1] );
		$writer->endTag( [ $dc, 'title' ] );
	$writer->endTag( [ $lvs, 'ExamCourse' ] );
}

close FILE;

### TYP.DAT ###

open FILE, $datfile_dir . $config->get_Lehrveranstaltungstypen or die $!;

while ( my $line = <FILE> ) {
	@linecontents = split( /\t/, $line );
	$writer->startTag( [ $lvs, 'CourseType' ], [ RDF, 'about'] => $lvs . 'coursetype#' . @linecontents[0] );
		$writer->startTag( [ $dc, 'title' ] );
			$writer->characters( @linecontents[2] );
		$writer->endTag( [ $dc, 'title' ] );
	$writer->endTag( [ $lvs, 'CourseType' ] );
}

close FILE;

### KATEGORIE.DAT ###

open FILE, $datfile_dir . $config->get_Lehrveranstaltungskategorien or die $!;

while ( my $line = <FILE> ) {
	@linecontents = split( /\t/, $line );
	$writer->startTag( [ $lvs, 'CourseCategory' ], [ RDF, 'about'] => $lvs . 'coursecategory#' . @linecontents[1] );
		$writer->startTag( [ $dc, 'title' ] );
			$writer->characters( @linecontents[2] );
		$writer->endTag( [ $dc, 'title' ] );
		$writer->startTag( [ $lvs, 'CourseCategoryInternalID' ] );
				$writer->characters( @linecontents[0] );
		$writer->endTag( [ $lvs, 'CourseCategoryInternalID' ] );
	$writer->endTag( [ $lvs, 'CourseCategory' ] );
}

close FILE;


### LVA.DAT ###

open FILE, $datfile_dir . $config->get_Lehrveranstaltungen or die $!;

while ( my $line = <FILE> ) {

	@linecontents = split( /\t/, $line );

	open(TITLE, "./data/dat/titel.dat");
	@titles=<TITLE>;
	close (TITLE);	
		
	my $searchtitle = @linecontents[0]; 
	my $titleFull = '';         
	                                                              
	foreach $titleline (@titles){
		if ($titleline =~ /$searchtitle\t/){
			@titlelinecontents = split( /\t/, $titleline );
			$titleFull = @titlelinecontents[1];                                      
		}
	}	

	$writer->startTag( [ $lvs, 'Course' ], [ RDF, 'about'] => $lvs . 'course#' . @linecontents[0] . '-' . @linecontents[1] . '-' . @linecontents[2] );
		$writer->startTag( [ $lvs, 'CourseTitle' ] );
			$writer->characters( $titleFull );
		$writer->endTag( [ $lvs, 'CourseTitle' ] );
		
		$writer->emptyTag( [ $lvs, 'CourseHasType' ], [ RDF, 'resource'] => $lvs . 'coursetype#' . @linecontents[1] );
		$writer->startTag( [ $lvs, 'ECTSCount' ] );
			$writer->characters( @linecontents[2] );
		$writer->endTag( [ $lvs, 'ECTSCount' ] );
		$writer->startTag( [ $lvs, 'SemesterHours' ] );
			$writer->characters( @linecontents[3] );
		$writer->endTag( [ $lvs, 'SemesterHours' ] );
		
		$writer->startTag( [ $lvs, 'CourseOccurrences' ], [ RDF, 'parseType'] => 'Collection');
			@courseOccurrences = split( /,/, @linecontents[4] );
			
			### LOAD FILES FOR URI RELATIONSHIP RESOLVEMENT ###
			open(STUDY, "./data/dat/studium.dat");
			@studies=<STUDY>;
			close (STUDY);
			
			open(CATEGORY, "./data/dat/kategorie.dat");
			@categories=<CATEGORY>;
			close (CATEGORY);	
		
			
			
		 ### WALK THROUGH ALL OCCURRENCES OF THE CURRENT COURSE ###
                       foreach my $courseOccurrence (@courseOccurrences) {
                               
                               my $studyExternalID = '';
                               my $categoryExternalID = '';
                               my $proposedSemester = substr($courseOccurrence,2,1 );
                               my $examCourseID = substr($courseOccurrence,3,10 );
                               
                               
                       ### SEARCH EXTERNAL ID FOR INTERNAL STUDY ID ###
                               my $searchstudy = substr($courseOccurrence,0,1 );
                                                               
                               foreach $studyline (@studies){
                                       if ($studyline =~ /$searchstudy\t/){
                                               @studylinecontents = split( /\t/, $studyline );
                                               $studyExternalID = @studylinecontents[1];                                      
                                       }
                               }
                               
                               ### SEARCH EXTERNAL ID FOR INTERNAL CATEGORY ID ###
                               my $searchcategory = substr($courseOccurrence,0,2 );
                               
                               foreach $categoryline (@categories){
                                       if ($categoryline =~ /$searchcategory\t/){
                                               @categorylinecontents = split( /\t/, $categoryline );
                                               $categoryExternalID = @categorylinecontents[1];                                
                                       }
                               }
                               
                               $writer->startTag( [ $lvs, 'CourseOccurrence'], [ RDF, 'about'] => $lvs . 'courseOccurrence/' . $studyExternalID . '-' .
                                                                       $categoryExternalID . '-' . $proposedSemester . '-' . $examCourseID);
                                       
                                       ##########################################################################
                                       #  Consider adding # insted / to study/ and coursecategory ...
                                       ####################################################################
                                       $writer->emptyTag( [ $lvs, 'CourseOccurrenceHasStudy' ], [ RDF, 'resource'] => $lvs . 'study/' . $studyExternalID);
                                       $writer->emptyTag( [ $lvs, 'CourseOccurrenceHasCategory' ], [ RDF, 'resource'] => $lvs . 'coursecategory/' . $categoryExternalID);
                                       $writer->emptyTag( [ $lvs, 'ProposedSemester' ], [ $lvs, 'proposedSemester' ] => $proposedSemester );
                                       $writer->emptyTag( [ $lvs, 'CourseOccurrenceHasExamCourse' ], [ RDF, 'resource'] => $lvs . 'examcourse#' . $examCourseID);
                                       
                                       ### CREATE RANDOM NUMBER OF 0 OR 1 FOR "IS COURSE MANDATORY?" ###
                                       $writer->startTag( [ $lvs, 'CourseMandatoryInCourseOccurrence' ]);
                                               $writer->characters( sprintf ("%.0f", rand(1)) );
                                       $writer->endTag( [ $lvs, 'CourseMandatoryInCourseOccurrence' ]);
                               $writer->endTag( [$lvs, 'CourseOccurrence']);
                       }
               $writer->endTag( [ $lvs, 'CourseOccurrences' ] );
	
	if (@linecontents[5] ne "\n" and @linecontents[5] ne "") {
		$writer->startTag( [ $lvs, 'Divisions' ], [ RDF, 'parseType'] => 'Collection');
	
		@divisions = split( /,/, @linecontents[5] );
		foreach my $division (@divisions) {
			$writer->startTag( [ $lvs, 'Division' ] );
				$writer->startTag( [ $dc, 'title' ] );
					$writer->characters( $division );
				$writer->endTag( [ $dc, 'title' ] );
			$writer->endTag( [ $lvs, 'Division' ] );
		}
		$writer->endTag( [ $lvs, 'Divisions' ] );
	}
	
	
	$writer->endTag( [ $lvs, 'Course' ] );
}

close FILE;

### LVA-INSTANZ.DAT ###

open FILE, $datfile_dir . $config->get_LehrveranstaltungsInstanzen or die $!;

while ( my $line = <FILE> ) {
	@linecontents = split( /\t/, $line );
	$writer->startTag( [ $lvs, 'CourseInstance' ], [ RDF, 'about'] => $lvs . 'courseinstance#' . @linecontents[0] );
		$writer->emptyTag( [ $lvs, 'InstanceOfCourse' ], [RDF, 'resource'] => $lvs . 'course#' . @linecontents[1]);
		$writer->startTag( [ $lvs, 'CourseInstanceYear' ] );
			$writer->characters( @linecontents[2] );
		$writer->endTag( [ $lvs, 'CourseInstanceYear' ] );
		$writer->startTag( [ $lvs, 'CourseInstanceSemester' ] );
			$writer->characters( @linecontents[3] );
		$writer->endTag( [ $lvs, 'CourseInstanceSemester' ] );
		$writer->startTag( [ $lvs, 'CourseInstanceBegin' ] );
			$writer->characters( @linecontents[4] );
		$writer->endTag( [ $lvs, 'CourseInstanceBegin' ] );
		$writer->startTag( [ $lvs, 'CourseInstanceEnd' ] );
			$writer->characters( @linecontents[5] );
		$writer->endTag( [ $lvs, 'CourseInstanceEnd' ] );
		$writer->startTag( [ $lvs, 'CourseInstanceLecturer' ] );
			$writer->characters( @linecontents[6] );
		$writer->endTag( [ $lvs, 'CourseInstanceLecturer' ] );
	$writer->endTag( [ $lvs, 'CourseInstance' ] );
}

close FILE;


$writer->endTag( [ RDF, 'RDF' ] );
$writer->end();
#$output->close();		# uncomment if you want to write to a file